package com.example.rentalcar.service.impl;

import com.example.rentalcar.entity.Users;
import com.example.rentalcar.mapper.UsersMapper;
import com.example.rentalcar.util.JwtUtil;
import com.example.rentalcar.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {

    private final UsersMapper user1Mapper;
    private final JwtUtil jwtUtil;

    @Autowired
    public UserServiceImpl(UsersMapper user1Mapper, JwtUtil jwtUtil) {
        this.user1Mapper = user1Mapper;
        this.jwtUtil = jwtUtil;
    }

    public Users findById(Long userId) {
        return user1Mapper.findById(userId);
    }

    public List<Users> findAll(int start, int limit) {
        return user1Mapper.findAll(start, limit);
    }

    public Users findByUsername(String userName) {
        return user1Mapper.findByUsername(userName);
    }

    public Users findByUserPhone(String userPhone) {
        return user1Mapper.findByUserPhone(userPhone);
    }

    public void save(Users user) {
        user1Mapper.save(user);
    }

    public void update(Users user) {
        user1Mapper.update(user);
    }

    public void deleteById(Long userId) {
        user1Mapper.deleteById(userId);
    }

    public String loginAndGenerateToken(String userName, String userPassword) {
        Users user = user1Mapper.findByUsernameAndPassword(userName, userPassword);
        if (user != null) {
            // 登录成功，生成并返回token
            return jwtUtil.generateToken(userName, user.getUserStu());
        }
        return null; // 登录失败
    }
}
